﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Super.Common.Model.Security;
using Super.Domain.Security;

namespace Super.Web.SecurityClearance
{
    public class SecurityProvider
    {
        private SecurityService _securityService;

        public SecurityProvider()
        {
            _securityService = new SecurityService();
        }

        public void SignIn(int userId)
        {
            TimeSpan span = TimeSpan.FromDays(1);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(userId.ToString(), false, Convert.ToInt32(span.TotalMinutes));
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            HttpContext.Current.Response.SetCookie(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket));
            SetupPrincipal(userId);
        }

        public void SetupPrincipal(int userId)
        {
            SetupPrincipal(_securityService.GetUser(userId));
        }

        private void SetupPrincipal(UserDTO userInfo)
        {
        }

        public UserDTO AuthenticateUser(string userName, string password, string domain)
        {
            if (_securityService.AuthenticateUser(userName, password, domain))
            {
                return _securityService.FindUser(userName, domain);
            }
            else
            {
                return null;
            }
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}